package com.jzo2o.customer.controller.consumer;

import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.model.Result;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author itlxc
 * @since 2024-10-14
 */
@RestController("consumerAddressBookController")
@RequestMapping("/consumer/address-book")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "用户端 - 地址簿相关接口")
public class AddressBookController {

    private final IAddressBookService addressBookService;


    @PostMapping
    @ApiOperation("新增地址簿")
    public Result add(@RequestBody AddressBookUpsertReqDTO dto) {
        log.info("新增地址簿参数：{}", dto);
        addressBookService.add(dto);
        return Result.ok();
    }

    @GetMapping("/page")
    @ApiOperation("地址薄分页查询")
    public PageResult<AddressBook> pageQuery(AddressBookPageQueryReqDTO dto) {
        log.info("地址薄分页查询参数：{}", dto);
        return addressBookService.pageQuery(dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("地址薄详情")
    public AddressBook getById(@PathVariable("id") Long id) {
        log.info("地址薄详情参数：{}", id);
        return addressBookService.getById(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("地址薄修改")
    public Result updateAddressBook(@PathVariable("id") Long id, @RequestBody AddressBookUpsertReqDTO dto) {
        log.info("地址薄修改参数：{}", dto);
        addressBookService.updateAddressBook(id, dto);
        return Result.ok();
    }

    @DeleteMapping("/batch")
    @ApiOperation("地址簿批量删除")
    public Result batchDelete(@RequestBody List<Long> ids) {
        log.info("地址簿批量删除参数：{}", ids);
        addressBookService.batchDelete(ids);
        return Result.ok();
    }

    @PutMapping("/default")
    @ApiOperation("设置/取消默认地址")
    public Result setIsDefault(Integer flag, Long id) {
        log.info("设置/取消默认地址参数：{}, {}", flag, id);
        addressBookService.setIsDefault(flag, id);
        return Result.ok();
    }

    @GetMapping("/defaultAddress")
    @ApiOperation("获取默认地址")
    public AddressBook getDefaultAddress() {
        log.info("获取默认地址");
        return addressBookService.getDefaultAddress();
    }
}
